package com.qp.basics.estivate;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 字段属性包装类。 很多数据库都是将字段的属性保存在表里的， 为了照顾到数据库的特性，我准备使用sql语句从数据库中获取字段属性。
 * 
 * 不同的数据库有不同的属性，即便相同性质的属性， 其属性名也可能不同，所以我准备在这里放一个Map保存所有的属性， 然后定义一些方法来体现所有的共同性。
 * 
 * 
 * @author PengJu Ma
 *
 */
public class Column {
	Map properties = new HashMap();
	private final Map<Column, RelationShip> parents = new HashMap<Column, RelationShip>();
	private final Map<Column, RelationShip> children = new TreeMap<Column, RelationShip>(
			new ColumnComparator());
	Table table;
	// 字段名称
	String columnName;
	// 字段类型的名称
	String dataType;
	// 字段类型的定义里的字段大小属性，如float(12,5)，那么这个变量的值就是12,5，
	// varchar2(300)，那么这个值就是300，如果是datetime，那么就是null。
	String columnSize;
	boolean isNullable;
	String extra;
	boolean isPk;
	/** 注释 */
	boolean idAutoIncrement;
	String comment;
	// 默认值.
	Object defaultValue;

	public Column(String columnName) {
		this.columnName = columnName;
	}

	public Column() {
	}

	public Map getProperties() {
		return properties;
	}

	public void putProperties(Map properties) {
		this.properties.putAll(properties);
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public Table getTable() {
		return table;
	}

	public void setTable(Table table) {
		this.table = table;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getColumnSize() {
		return columnSize;
	}

	public void setColumnSize(String columnSize) {
		this.columnSize = columnSize;
	}

	public Integer getLength() {
		if (this.columnSize == null)
			return null;
		int commaIndex = this.columnSize.indexOf(',');
		if (commaIndex > 0) {
			return Integer.parseInt(this.columnSize.substring(0, commaIndex));
		}
		try {
			return Integer.parseInt(this.columnSize);
		} catch (Exception e) {
		}
		return 0;
	}

	public boolean isNullable() {
		return isNullable;
	}

	public boolean getIsNullable() {
		return isNullable;
	}

	public void setNullable(boolean isNullable) {
		this.isNullable = isNullable;
	}

	public boolean isPk() {
		return isPk;
	}

	public void setPk(boolean isPk) {
		this.isPk = isPk;
	}

	public Object getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(Object defaultValue) {
		this.defaultValue = defaultValue;
	}

	public boolean isIdAutoIncrement() {
		return idAutoIncrement;
	}

	public void setIdAutoIncrement(boolean idAutoIncrement) {
		this.idAutoIncrement = idAutoIncrement;
	}

	public void setProperties(Map properties) {
		this.properties = properties;
	}

	public String getExtra() {
		return extra;
	}

	public void setExtra(String extra) {
		this.extra = extra;
	}

	public void addParent(Column parent, RelationShip constraint) {
		parents.put(parent, constraint);
		// table.addedParent(); TODO
	}

	/**
	 * Remove the specified parent column from this column
	 *
	 * @param parent
	 */
	public void removeParent(Column parent) {
		parents.remove(parent);
	}

	public void addChild(Column child, RelationShip constraint) {
		children.put(child, constraint);
		// table.addedChild(); TODO
	}

	/**
	 * Remove the specified child column from this column
	 *
	 * @param child
	 */
	public void removeChild(Column child) {
		children.remove(child);
	}

	private class ColumnComparator implements Comparator<Column> {
		public int compare(Column column1, Column column2) {
			int rc = column1.getTable().compareTo(column2.getTable());
			if (rc == 0)
				rc = column1.getColumnName().compareToIgnoreCase(
						column2.getColumnName());
			return rc;
		}
	}
}
